﻿// 文件名：函数调用
// 创建时间：2021/6/21/星期一 8:47:10

#include <iostream>
using namespace std;



//lotto()函数实现 P211函数功能
long double probability(unsigned numbers, unsigned picks);
void lotto()
{
	double total, choices;
	cout << "Enter the total number of choices on the game card and\n"
		"the number of picks allowed:\n";
	while ((cin >> total >> choices) && (choices <= total))
	{
		cout << "You have one chance in ";
		cout << probability(total, choices); // compute the odds
		cout << " of winning.\n";
		cout << "Next two numbers (q to quit): ";
	}
	cout << "Bye\n";

}
//计算中奖概率
long double probability(unsigned numbers, unsigned picks)
{
	long double result = 1.0;
	long double n;
	unsigned p;
	for (n = numbers, p = picks; p > 0; n--, p--)
	{
		result = result*n / p;
	}
	return result;
}


//演示数组函数的运作
const int ArSize = 8;
int sum_arr(int arr[], int n);
void arrfun2()
{
	int cookies[ArSize] = { 1,2,4,8,16,32,64,128 };
	cout << (int)cookies << " = array address,\n";
	cout << sizeof(cookies) << " = size of cookies\n";//此处的数组名能够代表整个数组
	int sum = sum_arr(cookies, ArSize);
	cout << "Total cookies eaten: " << sum << endl;
	sum = sum_arr(cookies, 3);
	cout << "First three eaters ate " << sum << endl;
	sum = sum_arr(cookies + 4, 4);
	cout << "Last four eaters ate " << sum << endl;
}
int sum_arr(int arr[], int n) // const并不代表arr中的变量是常量，只代表不可以修改数组元素
{
	int total = 0;
	cout << sizeof(arr) << " = size of arr\n"; //传入的是数组首地址，长度是指针变量的长度，指针本身并不能指示数组的长度
	cout << (int)arr << " = arr address\n";
	for (int i = 0; i < n; i++)
	{
		total += arr[i];
	}
	return total;
}



//填充数组的函数
void fillArray()
{
	const int len = 10;
	double arr[len];
	
	double temp;
	int i;
	for (i = 0; i < len; i++)
	{
		cout << "Enter value #" << i + 1 << ": ";
		cin >> temp;
		cout << endl;
		if (!cin)
		{
			cin.clear();
			while (cin.get() != '\n')
			{
				cout << i << endl;
				continue; //这么做的原因是有时候会在一行同时输入多个数字，事实上这种情况是合法的
				/*
				输入：65 84 .. 85
				输出：

					Enter value #2:
					Enter value #3:
					2
					2
					2
					2
					Bad input;input process terminated.
					arr[0]65        arr[1]84
				*/
			}
			cout << "Bad input;input process terminated.\n";
			break;
		}
		else if (temp < 0)
		{
			break;
		}
		arr[i] = temp;
	}
	for (int j = 0; j < i; j++)
	{
		cout << "arr[" << j << "]" << arr[j] << '\t';
	}
}


//实现检查用户输入是否为数字，之后调用其它函数
const int MAX = 5;
int fillArray2(double arr[], int limit); // 赋值，填充数组
void showArray(const double arr[], int n);
void revalue(double arr[], int n, double redio);
void arrfun3()
{
	double properties[MAX];
	int size = fillArray2(properties, MAX); //fillArray函数重载
	showArray(properties, size);
	if (size != 0)
	{
		cout << "Enterrevaluation factor: ";
		double factor;
		while (!(cin >> factor)) // bad input
		{
			cin.clear();
			while (cin.get() != '\n')
			{
				continue;
			}
			cout << "Bad input; please input a number: ";
		}
		revalue(properties, size, factor);
		showArray(properties, size);
	}
	cout << "Done\n";
	cin.get();
	cin.get();


}
int fillArray2(double arr[],int limit) // 赋值，填充数组
{
	double temp;
	int i;
	for (i = 0; i < limit; i++)
	{
		cout << "Enter value #" << i + 1 << ": ";
		cin >> temp;
		if (!cin)
		{
			cin.clear();
			while (cin.get() != '\n')
			{
				continue; //这么做的原因是有时候会在一行同时输入多个数字，事实上这种情况是合法的
			}
			cout << "Bad input;input process terminated.\n";
			break;
		}
		else if (temp < 0)
		{
			break;
		}
		arr[i] = temp;
	}
	return i;
}
void showArray(const double arr[], int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << "Property # " << (i + 1) << ": $";
		cout << arr[i] << endl;
	}
}
void revalue(double arr[], int n, double redio)
{
	for (int i = 0; i < n; i++)
	{
		arr[i] *= redio;
	}
}

int main()
{

	//调用lotto实现计算中彩票概率
	//lotto();

	//调用arrfun2()演示数组函数
	//arrfun2();

	//调用fillArray函数填充数组
	//fillArray();

	//调用arrfun3()函数综合各功能
	arrfun3();

	system("pause");
	return 0;
}